﻿using System;

namespace RFEngine.Threading
{
    public sealed class GameTimer
    {
        #region Fields

        private float _StartTime;
        private float _AccumulatedTime;

        private bool _IsRunning = false;

        #endregion

        #region Properties

        /// <summary>
        /// Gets the Total amount of Accumulated time in seconds.
        /// </summary>
        public float ElapsedSeconds
        {
            get
            {
                if (_IsRunning)
                    _AccumulatedTime += TimeManager.ElapsedGameTime - _StartTime;

                return _AccumulatedTime;
            }
        }

        /// <summary>
        /// Gets the Total amount of Accumulated time in miliseconds.
        /// </summary>
        public float ElapsedMiliseconds
        {
            get { return (float)System.Math.Round(ElapsedSeconds * 1000.0f, 2); }
        }

        /// <summary>
        /// Gets the Total amount of accumulated time in ticks
        /// </summary>
        public float ElapsedTicks
        {
            get { return (float)System.Math.Round(ElapsedMiliseconds * 1000.0f, 0); }
        }

        /// <summary>
        /// Gets a value indicating the State of this Timer.
        /// </summary>
        public bool IsRunning
        {
            get { return _IsRunning; }
        }

        #endregion

        #region Methods

        #region Constructor(s)

        /// <summary>
        /// Constructs a new instance of this object.
        /// </summary>
        public GameTimer() { }

        #endregion

        /// <summary>
        /// Method for Resetting this Timer back to Zero.
        /// </summary>
        public void Reset()
        {
            _AccumulatedTime = 0.0f;

            if (_IsRunning)
                _StartTime = TimeManager.ElapsedGameTime;
        }

        /// <summary>
        /// Method for Starting this Timer.
        /// </summary>
        public void Start()
        {
            if (!_IsRunning)
            {
                _StartTime = TimeManager.ElapsedGameTime;
                _IsRunning = true;
            }
        }

        /// <summary>
        /// Method for Stopping this Timer.
        /// </summary>
        public void Stop()
        {
            if (_IsRunning)
            {
                _AccumulatedTime += TimeManager.ElapsedGameTime - _StartTime;
                _IsRunning = false;
            }
        }

        #endregion
    }
}
